def format_element(bfo):
	"""
	Prints abstracts in all available languages (520__a - abstract text, 520__9 - abstract language)
	in compact view with javascript language switch.
	JQuery must be imported in the template
	"""
	abstracts = bfo.fields("520")
	if len(abstracts) == 0:
		return ""
	if len(abstracts) == 1:
		return abstracts[0]["a"]
	if len(abstracts) > 1 and abstracts[0]["a"] == abstracts[1]["a"]:
		return abstracts[0]["a"]
	lang = bfo.field("04107a")
	alist = []
	for abstract in abstracts:
		if abstract["9"] == lang:
			alist.insert(0, (abstract["9"], abstract["a"]))
		else:
			alist.append((abstract["9"], abstract["a"]))
	res = """<script type="text/javascript">
			 	$(document).ready(function(){
				 	$(".abslang").bind("click", function(){
				 		$(".abslang").css({"color" : "grey", "cursor" : "pointer"});
				 		$(this).css({"color" : "black", "cursor" : "default"});
				 		var target = $(this).attr('data-target');
				 		$(".abstract").css("display", "none");
				 		$("#"+target).css("display", "inline");
				 		
				 	});
				 	
				 	$(".abslang:first").trigger("click");
				 });
			 </script>"""
	if len(alist) > 1:
		c = 0
		for item in alist:
			res += "<span class='abslang' data-target='lang-%d' style='color:grey;'>[%s]</span> " % (c, item[0])
			c += 1
		res += "<br />\n"
	c = 0
	for item in alist:
		res += "<span class='abstract' id='lang-%d' style='display:none;'>%s</span>\n" % (c, item[1])
		c += 1
	return res
	
if __name__ == "__main__":
	from invenio.bibformat_engine import BibFormatObject as BFO
	print format(BFO(97139))


